package com.report.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Org;
import com.entity.Terminal;
import com.file.entity.FileTerminal;
import com.file.service.TerminalService;
import com.interfaces.OrgDao;
import com.interfaces.TerminalDao;
import com.report.entity.OnlineTerminal;
import com.utils.JsonResult;
import com.redis.service.Onlineservice;;

/**
 * @Title: OnlineterminalPReportService.java
 * @Package com.report.service
 * @author 孙梦楠
 * @date 2017年9月28日 上午9:57:34
 * @version V1.0.0
 */
@Service
public class OnlineterminalReportService {

	@Autowired
	private TerminalDao terminaldao;

	@Autowired
	private OrgDao orgdao;
	
	@Autowired
	private TerminalService terminalservice;
	
	@Autowired
	private Onlineservice Onlineservice;

//	// 列出数据库所有的在用终端
//	public List<OnlineTerminal> listAllTernimal() {
//		List<OnlineTerminal> onlineTerminals = new ArrayList<OnlineTerminal>();
//		List<Terminal> terminals = terminaldao.findByTerminalstatusNot("1");
//
//		for (Terminal terminal : terminals) {
//			OnlineTerminal terminal2 = new OnlineTerminal();
//			terminal2.setTerminaladdress(terminal.getTerminaladdress());
//			terminal2.setStatus("0");// 默认未在线
//			onlineTerminals.add(terminal2);
//		}
//
//		return onlineTerminals;
//	}

	// 饼图报表
	public JsonResult<Object[][]> onlineterminalReport(String opeorgcookie, String opetypecookie) {
		Object[][] obj = new Object[3][2];
		List<OnlineTerminal> onlineTerminals = new ArrayList<OnlineTerminal>();
		List<Terminal> terminals = new ArrayList<Terminal>();
		
		if (opeorgcookie.equals("1")) {
			terminals = terminaldao.findByTypeAndTerminalstatusNot(opetypecookie, "4");
			for (Terminal terminal : terminals) {
				OnlineTerminal terminal2 = new OnlineTerminal();
				terminal2.setTerminaladdress(terminal.getTerminaladdress());
				terminal2.setStatus("0");// 默认未在线
				onlineTerminals.add(terminal2);
			}
		} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
			List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
			for (Org org : orgs) {
				terminals = terminaldao.findByTerminalorgAndTypeAndTerminalstatusNot(
						Integer.parseInt(org.getOrgvalue()), opetypecookie, "4");
				for (Terminal terminal : terminals) {
					OnlineTerminal terminal2 = new OnlineTerminal();
					terminal2.setTerminaladdress(terminal.getTerminaladdress());
					terminal2.setStatus("0");// 默认未在线
					onlineTerminals.add(terminal2);
				}
			}
		} else {
			terminals = terminaldao.findByTerminalorgAndTypeAndTerminalstatusNot(Integer.valueOf(opeorgcookie),
					opetypecookie, "4");
		for (Terminal terminal : terminals) {
			OnlineTerminal terminal2 = new OnlineTerminal();
			terminal2.setTerminaladdress(terminal.getTerminaladdress());
			terminal2.setStatus("0");// 默认未在线
			onlineTerminals.add(terminal2);
		}
		}

		List<OnlineTerminal> onlineTerminals2 = Onlineservice.getOnline(onlineTerminals);
		int i = 0;// 不在线
		int j = 0;// 在线 空闲
		int k = 0;// 在线 通讯
		for (OnlineTerminal onlineTerminal : onlineTerminals2) {
			if (onlineTerminal.getStatus().equals("0")) {
				i++;
			} else if (onlineTerminal.getStatus().equals("1")){
				j++;
			} else {
				k++;
			}
		}
		obj[0][0] = "未在线";
		obj[1][0] = "空闲中";
		obj[2][0] = "通讯中";
		obj[0][1] = i;
		obj[1][1] = j;
		obj[2][1] = k;

		return new JsonResult<Object[][]>(701, "饼图报表", obj);
	}
	
	
	//不在线终端表格
	public JsonResult<List<FileTerminal>> listOfflineTerminal(String opeorgcookie, String opetypecookie,String languagecookie,String status){
		List<OnlineTerminal> onlineTerminals = new ArrayList<OnlineTerminal>();
		List<Terminal> terminals = new ArrayList<Terminal>();
		if (opeorgcookie.equals("1")) {
			terminals = terminaldao.findByTypeAndTerminalstatusNot(opetypecookie, "4");
			for (Terminal terminal : terminals) {
				OnlineTerminal terminal2 = new OnlineTerminal();
				terminal2.setTerminaladdress(terminal.getTerminaladdress());
				terminal2.setStatus("0");// 默认未在线
				onlineTerminals.add(terminal2);
			}
		} else if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
			List<Org> orgs = orgdao.findByOrgfather(opeorgcookie);
			for (Org org : orgs) {
				terminals = terminaldao.findByTerminalorgAndTypeAndTerminalstatusNot(
						Integer.parseInt(org.getOrgvalue()), opetypecookie, "4");
				for (Terminal terminal : terminals) {
					OnlineTerminal terminal2 = new OnlineTerminal();
					terminal2.setTerminaladdress(terminal.getTerminaladdress());
					terminal2.setStatus("0");// 默认未在线
					onlineTerminals.add(terminal2);
				}
			}
		} else {
			terminals = terminaldao.findByTerminalorgAndTypeAndTerminalstatusNot(Integer.valueOf(opeorgcookie),
					opetypecookie, "4");
			for (Terminal terminal : terminals) {
				OnlineTerminal terminal2 = new OnlineTerminal();
				terminal2.setTerminaladdress(terminal.getTerminaladdress());
				terminal2.setStatus("0");// 默认未在线
				onlineTerminals.add(terminal2);
			}
		}
		
		List<FileTerminal> fileterminals = new ArrayList<FileTerminal>();
		List<OnlineTerminal> onlineTerminals2 = Onlineservice.getOnline(onlineTerminals);
		
		
		for (OnlineTerminal onlineTerminal : onlineTerminals2) {
			if(onlineTerminal.getStatus().equals(status)) {
				Terminal terminal=	terminaldao.findByTerminaladdress(onlineTerminal.getTerminaladdress());
				fileterminals.add(terminalservice.dofileterminal(terminal, languagecookie));
			}
		}
		return  new JsonResult<List<FileTerminal>>(702, "各种状态设备列表", fileterminals);
		
	}
	

}
